package cn.dduan.fool.collection.list;

import java.util.List;
import java.util.Vector;

/**
 * Vector
 * <p>
 * 优点: 底层数据结构是数组，查询快，增删慢
 * <p>
 * 缺点: 线程安全，效率低
 *
 * @author dduan
 * @version 1.0
 * @date 2023/3/27
 */
public class MyVector {
    public static void main(String[] args) {
        MyVector.threadSafe();
    }

    /**
     * Vector 线程安全
     *
     * @author dduan
     * @version 1.0
     * @date 2025/3/7
     */
    private static void threadSafe() {
        List<Integer> list = new Vector<>();

        // 采用匿名内部类实现多线程
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    list.add(i);
                }
            }
        });
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    list.add(i);
                }
            }
        });
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    list.remove(i);
                }
            }
        });
        t3.start();

        for (int i = 0; i < 100; i++) {
            list.add(i);
        }

        System.out.println(list.toString());
    }
}
